<template>
	<view>
		<view class="texts">分社数据({{data.total}}人)</view>
		<loadstatusVue :status="data.loadStatus">
			<view class="contents">
				<view class="item flex_jc_sb" v-for="(item, index) in data.list" :key="index" style="height: 120rpx">
					<view class="items_l flex">
						<image v-if="item.img" :src="item.img" mode="aspectFill"></image>
						<image v-else src="https://www.daolangtongge.com/read-book-api/profile/mini_app/img/head.png" mode="aspectFill"></image>
						<view class="">
							<view class="flex">
								<view class="t1">{{ item.name }}</view>
								<view class="t2" v-if="item.vipType">{{ $userStatus(item.vipType) }}{{ item.id }}</view>
							</view>
							<view class="t3">到期时间：{{ item.vipTimeEnd }}</view>
						</view>
					</view>
				</view>
			</view>
		</loadstatusVue>
	</view>
</template>

<script setup>
import { ref, reactive, getCurrentInstance } from 'vue';
import { onLoad, onShow, onReachBottom, onPageScroll } from '@dcloudio/uni-app';
import { changeUserList } from '@/api/branch/member.js';
import loadstatusVue from '@/components/loadstatus/loadstatus.vue';
import { toast, showConfirm } from '@/utils/common.js';
// 所有数据都定义在 data 中
const data = reactive({
	list: [],
	total:0,
	loadStatus: 'before', // 加载状态
	hasMore: true, // 是否有更多数据
	isLoading: false, // 防止重复请求的锁
	queryParams: {
		pageNum: 1,
		pageSize: 10
	}
});
// 延迟函数
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// 设置加载状态的方法
const setLoadStatus = (status, options = {}) => {
	data.loadStatus = status;
};
// 处理响应数据的通用方法
const handleResponseData = async (res, isLoadMore = false) => {
	// 如果是加载更多，确保loading状态至少显示500ms，避免闪烁
	if (isLoadMore) {
		await delay(500);
	}
	if (res.code == 200) {
		// 处理数据
		if (isLoadMore) {
			data.list.push(...res.rows);
		} else {
			data.list = res.rows || [];
		}
		// 判断是否有更多数据
		data.hasMore = res.rows && res.rows.length >= data.queryParams.pageSize;
		// 设置状态
		if (data.list.length === 0) {
			setLoadStatus('empty');
		} else if (!data.hasMore) {
			setLoadStatus('noMore');
		} else {
			setLoadStatus('success');
		}
		return true;
	}
	return false;
};
// 获取列表数据
const getList = async (isLoadMore = false) => {
	// 防止重复请求
	if (data.isLoading) return;
	try {
		data.isLoading = true;
		if (isLoadMore) {
			if (!data.hasMore) return;
			setLoadStatus('loading');
		} else {
			setLoadStatus('before');
			// 初始加载时显示loading至少1秒，避免闪烁
			await delay(500);
		}
		const res = await changeUserList(data.queryParams);
		const success = await handleResponseData(res, isLoadMore);
		if (!success) {
			// 请求失败的处理
			setLoadStatus(data.list.length === 0 ? 'empty' : 'success');
		}
	} catch (err) {
		await delay(500);
		setLoadStatus(data.list.length === 0 ? 'empty' : 'success');
	} finally {
		data.isLoading = false;
	}
};
onLoad(() => {
	getList();
});
// 上拉加载更多
onReachBottom(() => {
	if (data.hasMore && (data.loadStatus === 'success' || data.loadStatus === 'noMore') && !data.isLoading) {
		data.queryParams.pageNum++;
		getList(true);
	}
});
</script>
<style lang="scss">
page {
	background: #f7f8f6;
}
.texts {
	font-family: PingFang SC, PingFang SC;
	font-weight: 500;
	font-size: 24rpx;
	color: #75613e;
	margin: 20rpx;
}
.contents {
	.item {
		width: 710rpx;
		height: 112rpx;
		background: #ffffff;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		margin: 20rpx auto;
		padding: 20rpx;
		box-sizing: border-box;
		.items_l {
			image {
				width: 72rpx;
				height: 72rpx;
				border-radius: 40rpx;
				margin-right: 18rpx;
			}
			.t1 {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #1b1b1b;
			}
			.t2 {
				display: inline-block;
				height: 32rpx;
				background: rgba(216, 198, 167, 0.5);
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				border: 1px solid #75613e;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 20rpx;
				color: #75613e;
				line-height: 32rpx;
				text-align: center;
				margin-left: 15rpx;
				padding: 0rpx 15rpx;
			}
			.t3 {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 20rpx;
				color: #aeaeac;
				margin-top: 12rpx;
			}
		}
		.items_r {
			.btn {
				width: 68rpx;
				height: 44rpx;
				border-radius: 28rpx 28rpx 28rpx 28rpx;
				border: 1px solid #75613e;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 20rpx;
				color: #75613e;
				line-height: 44rpx;
				text-align: center;
				margin-left: 15rpx;
			}
		}
	}
}
</style>
